<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_plugins/caches.php';
require $_SERVER['DOCUMENT_ROOT'].'/../_interface/upload.php';

$db = new DB('star');

$title_id = isset($_POST['title_id']) ? h($_POST['title_id']) : '';
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$content = isset($_POST['content']) ? preg_replace('/\n/', '', preg_replace('/\r/', '', $_POST['content'])) : '';
$html = isset($_POST['html']) ? h($_POST['html']) : '';
$index_top = isset($_POST['index_top']) ? 2 : 1;
$orders = isset($_POST['orders']) ? intval($_POST['orders']) : 0;
$old_history_id = isset($_POST['old_history_id']) ? intval($_POST['old_history_id']) : 0;
$edit_uid = Sessions::get('uid');
$updated = time();

//检验必要参数
if( $title_id=='' || $title=='' || $old_history_id<1 ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}
if( $content=='' && $html=='' ){
	echo '<script>alert("内容和HTML两项至少要填一项！");window.history.go(-1);</script>';
	exit;
}

//检验标题是否重复
$star_sql = 'select title_id,thumb,index_thumb,title_history_id from star where title="'.$title.'" and is_del=1 limit 1';
$star_rs = $db->query($star_sql)->getOne();
if($star_rs['title_id']==true && $star_rs['title_id']!=$title_id){
	echo '<script>alert("修改失败，名人《'.$title.'》已存在！");window.history.go(-1);</script>';
	exit;
}

//上传处理
if($_FILES['attach']['size']['star']>0 || $_FILES['attach']['size']['news']>0){
	$up = new Upload($_FILES,'../../../star_upload/');     //创建
	$fs = $up->execute();    //开始执行，并返回数据
}

//名人榜头像
if($_FILES['attach']['size']['star']>0){
	if($fs['star']['flag'] == -2){
		echo '<script>alert("上传名人榜头像大于1M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['star']['flag'] == 1){//上传成功
		$star_images = $fs['star']['handle_dir'].$fs['star']['name'];
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$star_images = $star_rs['thumb'];
}

//新闻站头像
if($_FILES['attach']['size']['news']>0){
	if($fs['news']['flag'] == -2){
		echo '<script>alert("上传名人榜头像大于1M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['news']['flag'] == 1){//上传成功
		$news_images = $fs['news']['handle_dir'].$fs['news']['name'];
	}else{
		echo '<script>alert("图片上传出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$news_images = $star_rs['index_thumb'];
}

//生成index.html
$filepath = '/v/'.$title_id;
$filename = '/index.html';
if( $html=='' ){
	//更新内容
	$star_data=array('content'=>$content);
	$db->table('star')->where('title_id='.$title_id)->update($star_data)->run();
	
	$url = 'http://star.zgqw.com/show.php?title_id='.$title_id;
	Caches::toStatic( $url, 'www'.$filepath.$filename );
}else{
	Caches::mkdirs( $_SERVER['DOCUMENT_ROOT'].$filepath );
	file_put_contents( $_SERVER['DOCUMENT_ROOT'].$filepath.$filename, h_d($html) );
}

//上传附件
if($_FILES['attachment']['size']['attachment']>0){
	$up = new Upload($_FILES, '../../../../_cache/_webfiles/vip/'.$title_id.'/', 'zip', 10240, 'attachment');     //创建
	$fs = $up->execute();    //开始执行，并返回数据
	
	if($fs['attachment']['flag'] == -2){
		echo '<script>alert("上传附件大于10M！");window.history.go(-1);</script>';
		exit;
	}elseif($fs['attachment']['flag'] == 1){//上传成功
		$attachment = substr($fs['attachment']['handle_dir'],8).$fs['attachment']['name'];
	}else{
		echo '<script>alert("上传附件出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
	
	//解压
	$zip = new ZipArchive;
	if ($zip->open( $_SERVER['DOCUMENT_ROOT'].$attachment ) === TRUE) {
		$zip->extractTo( $_SERVER['DOCUMENT_ROOT'].'/v/'.$title_id.'/' );
		$zip->close();
	}else{
		echo '<script>alert("附件格式出错，请重新上传！");window.history.go(-1);</script>';
		exit;
	}
}

//获取文件内容(压缩包里可能有index.html)
$file_content = h( @file_get_contents( $_SERVER['DOCUMENT_ROOT'].$filepath.$filename ) );
$html = $file_content;

//更新版本号
$changes=false;
$history_sql = 'select history_id,static_url from star_history where title_id='.$title_id.' order by history_id desc limit 1';
$history_rs = $db->query($history_sql)->getOne();
if( $old_history_id != $star_rs['title_history_id'] ){
	$changes=true;
}
$title_history_id = $history_rs['history_id']+1;//生成新版本号

//组装star的update数组
$star_data=array('title'=>$title,
			'content'=>$content,
			'html'=>$html,
			'index_thumb'=>$news_images,
			'thumb'=>$star_images,
			'index_top'=>$index_top,
			'orders'=>$orders,
			'title_history_id'=>$title_history_id,
			'is_empty'=>2,
			'edit_uid'=>$edit_uid,
			'updated'=>$updated,
			);
$db->table('star')->where('title_id='.$title_id)->update($star_data)->run();//更新

//组装history的insert数组
$history_data=array('history_id'=>$title_history_id,
					'title_id'=>$title_id,
					'title'=>$title,
					'content'=>$content,
					'html'=>$html,
					'index_thumb'=>$news_images,
					'thumb'=>$star_images,
					'static_url'=>$history_rs['static_url'],
					'create_uid'=>$edit_uid,
					'created'=>$updated,
					);
$db->table('star_history')->insert($history_data)->run();//插入

$track_sql = 'select track_id from star_track where title_id='.$title_id.' order by track_id desc limit 1';
$track_rs = $db->query($track_sql)->getOne();
//组装track的insert数组
if($changes===true){//调用旧版本
	$track_data=array('track_id'=>($track_rs['track_id']+1),
				'title_history_id'=>$title_history_id,
				'title_id'=>$title_id,
				'create_uid'=>$edit_uid,
				'created'=>$updated,
				'changes'=>$old_history_id.'-'.$title_history_id,
				);
}else{
	$track_data=array('track_id'=>($track_rs['track_id']+1),
				'title_history_id'=>$title_history_id,
				'title_id'=>$title_id,
				'create_uid'=>$edit_uid,
				'created'=>$updated,
				'changes'=>$star_rs['title_history_id'].'-'.$title_history_id,
				);
}
$db->table('star_track')->insert($track_data)->run();//插入

echo '<script>alert("修改成功！");window.location.href="/www/admin/star/star_lists.php";</script>';
exit;
